package com.zhenhunfan.spring.boot.samples.generator;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.yaml.snakeyaml.Yaml;

import java.sql.Types;
import java.util.Collections;
import java.util.Map;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author zhanghong
 * @date 2023/11/14
 */
public class Generator {
    public static void main(String[] args) {
        Yaml yaml=new Yaml();
        Map<String,Object> map = yaml.load(ResourceUtil.readUtf8Str("application.yml"));
        JSONObject properties = JSONUtil.parseObj(map);
        String url = properties.getByPath("spring.datasource.url",String.class);
        String username = properties.getByPath("spring.datasource.username",String.class);
        String pwd = properties.getByPath("spring.datasource.password",String.class);
        FastAutoGenerator.create(url, username, pwd)
                .globalConfig(builder -> {
                    // 设置作者
                    builder.author("zhenhunfan")
                            .dateType(DateType.ONLY_DATE)
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .outputDir(System.getProperty("user.dir") + "\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    // 设置父包名
                    builder.parent("com.zhenhunfan.spring.boot.samples")
                            // 设置mapperXml生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "\\src\\main" +
                                    "\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    // 设置过滤表前缀
                    builder.addTablePrefix("sys_")
                            .addExclude("flyway_schema_history")
                            .entityBuilder().enableFileOverride()
                            .serviceBuilder().enableFileOverride()
                            .mapperBuilder().enableFileOverride()
                            .controllerBuilder().enableFileOverride();
                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
